package com.tedsoft.first_group_project;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


import com.tedsoft.framework.FileIO;

public class Settings {
	
	//wartosc kalibarcji dla ccelerometru
	public static float accel_Y_calibration_val = 0f;
	
	//margines z lewej strony ekranu dla stateczku
	public static int ship_left_margin = 60; 
	
	//polozenie i wymiar pola gry
	public static int gameFieldHeight = 230;
	public static int gameFieldBottomMargin = 20;
	
	//ustawienie odnosnie dzwieku
    public static boolean soundEnabled = true;
    
    //zmienna wlaczajaca widok opcji
    public static boolean opcje = false;
    //HIGHSCORES
    public static int[] highscores = new int[] { 100, 80, 50, 30, 10 };
    public enum GameState {
        Running,
        Paused,
        GameOver,
    };
	
	public static GameState state = GameState.Running;

    public static void load(FileIO files) {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(
                    files.readFile(".mrnom")));
            soundEnabled = Boolean.parseBoolean(in.readLine());
            for (int i = 0; i < 5; i++) {
                highscores[i] = Integer.parseInt(in.readLine());
            }
        } catch (IOException e) {
            // :( It's ok we have defaults
        } catch (NumberFormatException e) {
            // :/ It's ok, defaults save our day
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
    }

    public static void save(FileIO files) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    files.writeFile(".mrnom")));
            out.write(Boolean.toString(soundEnabled));
            out.write("\n");
            for (int i = 0; i < 5; i++) {
                out.write(Integer.toString(highscores[i]));
                out.write("\n");
            }

        } catch (IOException e) {
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }

    public static void addScore(int score) {
        for (int i = 0; i < 5; i++) {
            if (highscores[i] < score) {
                for (int j = 4; j > i; j--)
                    highscores[j] = highscores[j - 1];
                highscores[i] = score;
                break;
            }
        }
    }
    
    public boolean inBounds(int x1, int y1, int x, int y, int width, int height)
    {
        if(x1 > x && x1 < x + width - 1 && 
        	y1 > y && y1 < y + height - 1) 
            return true;
        else
            return false;
    }
}